import { createSlice, createAction } from "@reduxjs/toolkit";

interface StateProps {
  language: "zh" | "en" | "jp";
  languageList: Array<{
    name: string;
    code: string;
  }>;
}

const initialState: StateProps = {
  language: "zh",
  languageList: [
    {
      name: "中文",
      code: "zh",
    },
    {
      name: "英文",
      code: "en",
    },
  ],
};

// 生成Action签名
const languageChange = createAction("languageChange");
const addlanguage = createAction("addlanguage");

export const languageSlice = createSlice({
  name: "languageChangeToAdd",
  initialState,
  // map object对象映射，函数体为reducer处理中心，该函数体不是纯函数，无需生成新数据替换旧数据
  reducers: {
    [languageChange.type]: (state, action) => {
      state.language = action.payload;
    },
    [addlanguage.type]: (state, action) => {
      state.languageList = [...state.languageList, action.payload];
    },
  },
});
